/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package LabelPanelPackage;

import GUIUtilities.LabelManager;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * a panel that grows as you add more labels to it
 * @author Heather
 */
public class LabelPanel extends JPanel
{

  private static final int SCROLL_BAR_SIZE = 18;
  private int nextLabelY = 0;
  // how far from the left the label should be
  private final int defaultX = 2;
  private int maxLabelWidth = getSize().width;
  private boolean layoutSet = false;
  private Dimension containerScale = null;

  public void removeAll()
  {
    super.removeAll();
    nextLabelY = 0;
  }

  public void setSize(Dimension s)
  {
    super.setSize(s);
    if (s.width > maxLabelWidth)
    {
      maxLabelWidth = s.width;
    }
  }

  public void setSize(int x, int y)
  {
    super.setSize(x, y);
    if (y > maxLabelWidth)
    {
      maxLabelWidth = y;
    }
  }

  public void setPreferredSize(Dimension s)
  {
    super.setPreferredSize(s);
    if (s.width > maxLabelWidth)
    {
      maxLabelWidth = s.width;
    }
  }

  public void addLabel(JLabel l)
  {
    // make sure the layout is null or else the first label is off
    if (!layoutSet)
    {
      this.setLayout(null);
      layoutSet = true;
    }

    Dimension labelD = LabelManager.getLabelDimensions(l);
    Point labelP = new Point(defaultX, nextLabelY);
    add(l);
    l.setLocation(labelP);
    l.setSize(labelD);
    nextLabelY += labelD.height;

    grow();
  }

  // grow the panel if needed
  private void grow()
  {
    setMaxLabelWidth();
    int newHeight = getSize().height;
    if (nextLabelY >= getSize().height)
    {
      newHeight = newHeight * 2;
    }

    Dimension newSize = new Dimension(maxLabelWidth, newHeight);
    setSize(newSize);
    setPreferredSize(newSize);

    scaleAllLabels();
  }

  private void setMaxLabelWidth()
  {
    int currentMax = -1;
    for (Component c : this.getComponents())
    {
      if (c.getWidth() > currentMax)
      {
        currentMax = c.getWidth();
      }
    }

    maxLabelWidth = currentMax;
  }

  private void scaleAllLabels()
  {
    if (containerScale != null)
    {
      setScaleTo(containerScale);
    }
    else
    {
      scaleAllLabelsWithRespectToLargestLabel();
    }
  }

  private void scaleAllLabelsWithRespectToLargestLabel()
  {
    Component[] allComponents = this.getComponents();
    for (Component c : allComponents)
    {
      JLabel j = (JLabel) c;

      Dimension d = j.getSize();
      Dimension newD = new Dimension(this.getWidth(), d.height);
      j.setSize(newD);
    }
  }

  public void setScaleTo(Dimension scale)
  {
    containerScale = scale;
    Component[] allComponents = this.getComponents();
    for (Component c : allComponents)
    {
      JLabel j = (JLabel) c;

      Dimension d = j.getSize();
      Dimension newD = new Dimension((int)scale.getWidth() - SCROLL_BAR_SIZE, d.height);
      j.setSize(newD);
    }

  }
}
